//
//  CircleOfFriendsVC.m
//  qiuShiJiangHu
//
//  Created by xalo on 16/1/25.
//  Copyright © 2016年 糗事江湖项目组. All rights reserved.
//

#import "CircleOfFriendsVC.h"
#import "HttpHelper.h"
#import "HomePageController.h"
#import "UIImageView+WebCache.h"
#import "HomePageModel.h"
#import "ArticleDetailViewController.h"
#import "HttpHelperWithComment.h"
#import <MBProgressHUD.h>

@interface CircleOfFriendsVC ()

@end

@implementation CircleOfFriendsVC
-(void)viewWillAppear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationController.navigationBar.translucent = NO;
    
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
  [[HttpHelperWithComment sharedHttpHelper] fetchModelWithUrl:kURL block:^{
      [self.tableView reloadData];
      [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
  }];
     [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"640x130"] forBarMetrics:(UIBarMetricsDefault)];
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [[HttpHelperWithComment sharedHttpHelper] countOfModelArray];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"circleCell" forIndexPath:indexPath];
    
    HomePageModel *model = [[HttpHelperWithComment sharedHttpHelper] returnModelWithIndex:indexPath.row];
    
    cell.textLabel.text = model.login;
    NSString *url=[self fetchIconUrlWith:model];
    
    [cell.imageView sd_setImageWithURL:[NSURL URLWithString:url] placeholderImage:[UIImage imageNamed:@"Icon@2x"]];
    cell.imageView.layer.cornerRadius=25;
    cell.imageView.layer.masksToBounds=YES;
    
    // Configure the cell...
    
    return cell;
}
//获取头像的url
-(NSString *)fetchIconUrlWith:(HomePageModel *)model
{
    NSInteger number = model.Id;
    
    NSString *numStr = [NSString stringWithFormat:@"%ld",number];
    
    NSString *num1 = nil;
    
    if (numStr.length == 8) {
        
        num1 = [numStr substringWithRange:NSMakeRange(0, 4)];
        
    }else if(numStr.length == 7)
    {
        num1 = [numStr substringWithRange:NSMakeRange(0, 3)];
        
    }else if(numStr.length == 6)
    {
        num1 = [numStr substringWithRange:NSMakeRange(0, 2)];
        
    }else
    {
        num1 = [numStr substringWithRange:NSMakeRange(0, 1)];
        
    }
    NSString *url = [NSString stringWithFormat:@"http://pic.qiushibaike.com/system/avtnew/%@/%@/thumb/%@",num1,numStr,model.icon];
    return url;

}
#pragma mark--代理执行方法
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ArticleDetailViewController *articleDVC=[[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"ArticleDVC"];
    
    articleDVC.index=indexPath.row;
    articleDVC.ID=[[HttpHelperWithComment sharedHttpHelper] returnModelWithIndex:indexPath.row].ID;
    [self.navigationController pushViewController:articleDVC animated:YES];


}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
